home *** CD-ROM | disk | FTP | other *** search
/ MIDICraft's MIDINET CD-ROM / MIDICraft's MIDINET CD-ROM.iso / DOSUTILS / KORG / FILEI3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-27  |  1.5 KB  |  75 lines

  1. #include "filei3.hpp"
  2.  
  3. // 7 bytes to 8 codes
  4. int code7to8(unsigned char* s7, int len7, unsigned char* s8)
  5. {
  6.   if (!s7 || !s8 || len7 <= 0)
  7.     return 0;
  8.   if (len7 > 7)
  9.     len7 = 7;
  10.   unsigned char v = 0;
  11.   for (int i = 0; i < len7; i++)
  12.   {
  13.     v += (((s7[i] >> 7) & 1) << i);
  14.     s8[i+1] = s7[i] & 0x7f;
  15.   }
  16.   s8[0] = v;
  17.   return len7+1;
  18. }
  19.  
  20. // 8 codes to 7 bytes
  21. int code8to7(unsigned char* s8, int len8, unsigned char* s7)
  22. {
  23.   if (!s7 || !s8 || len8 <= 0)
  24.     return 0;
  25.   if (len8 > 8)
  26.     len8 = 8;
  27.   for (int i = 0; i < len8-1; i++)
  28.     s7[i] = s8[i+1] + (((s8[0] >> i) & 1) << 7);
  29.   return len8-1;
  30. }
  31.  
  32. // read bytes from file and produce codes
  33. int loaddata7(FILE* f, int pos, int len, unsigned char* data7)
  34. {
  35.   if (!f || len <= 0 || !data7)
  36.     return 0;
  37.   fseek(f, pos, SEEK_SET);
  38.   if (ftell(f) != pos)
  39.     return 0;
  40.   int len7 = 0;
  41.   int n, n8;
  42.   unsigned char buf[7];
  43.   while (len > 0)
  44.   {
  45.     n = 7; if (len < n) n = len;
  46.  
  47.     if (fread(buf, n, 1, f) != 1)
  48.       return 0;
  49.     n8 = code7to8(buf, n, data7);
  50.     data7 += n8;
  51.     len7 += n8;
  52.     len -= n;
  53.   }
  54.   return len7;
  55. }
  56.  
  57. // convert codes to bytes and write to file
  58. int savedata8(FILE* f, unsigned char* data8, int len8)
  59. {
  60. int len7 = 0;
  61. unsigned char buf[7];
  62.  
  63.   if (!f || !data8 || len8 <= 0)
  64.     return 0;
  65.   while (len8 >= 8)
  66.   {
  67.     int n = code8to7(data8, len8, buf);
  68.     if (fwrite(buf, n, 1, f) != 1)
  69.       break;
  70.     len7 += n;
  71.     len8 -= 8;
  72.   }
  73.   return len7;
  74. }
  75.